package com.goods.controller.business;

import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/business/productCategory")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;

    //加载分类数据
    @GetMapping("/categoryTree")
    public ResponseBean<PageVO<ProductCategoryTreeNodeVO>> getCategoryList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){

        PageVO<ProductCategoryTreeNodeVO> pageVO = productCategoryService.getCategoryList(pageNum,pageSize);
        return ResponseBean.success(pageVO);
    }

    //获取父类分级属性
    @GetMapping("/getParentCategoryTree")
    public ResponseBean getParentCategoryList(){
        List<ProductCategoryTreeNodeVO> treeNodeVOS = productCategoryService.getParentCategoryList();
        return ResponseBean.success(treeNodeVOS);
    }

    //添加分类
    @PostMapping("/add")
    public ResponseBean addProductCategory(@RequestBody ProductCategory productCategory){
        productCategoryService.addProductCategory(productCategory);
        return ResponseBean.success();
    }

    //编辑分类
    @GetMapping("/edit/{id}")
    public ResponseBean<ProductCategory> editProductCategory(@PathVariable Long id){
        ProductCategory productCategory = productCategoryService.editProductCategory(id);
        return ResponseBean.success(productCategory);
    }

    //修改分类
    @PutMapping("/update/{id}")
    public ResponseBean updateProductCategory(@PathVariable Long id,@RequestBody ProductCategory productCategory){
        productCategoryService.updateProductCategory(id,productCategory);
        return ResponseBean.success();
    }

    //删除分类
    @DeleteMapping("/delete/{id}")
    public ResponseBean deleteProductCategory(@PathVariable Long id){
        Integer flag = productCategoryService.deleteProductCategory(id);
        return ResponseBean.success();
    }
}
